package com.ywjh.inter.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.common.SystemConfig;
import com.ywjh.inter.entity.BindInsulinEntity;
import com.ywjh.inter.entity.DocProfileEntity;
import com.ywjh.inter.entity.InstitutionEntity;
import com.ywjh.inter.entity.ProfileEntity;
import com.ywjh.inter.entity.UserAddressEntity;
import com.ywjh.inter.entity.UsersEntity;
import com.ywjh.inter.service.InstService;
import com.ywjh.inter.service.InsulinService;
import com.ywjh.inter.service.UsersService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Profile extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7068714440569351896L;

	protected void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
			throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");

		JSONObject postData = StrUtil.getPostJSON(req);
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");
		int r = 0;
		String e = "";
		JSONObject json = new JSONObject();
		switch (act) {
		case "import":
			String phone= data.getString("phone");
			UsersEntity users = UsersService.getInstance().findUserByPhone(phone);
			int userid=users.getId();
			String name = data.getString("name");
			String idcard = data.getString("idcard");
			float high = StrUtil.getFloat(data.getString("high"), 0.0f);
			float weight = StrUtil.getFloat(data.getString("weight"), 0.0f);
			int bmi = data.getInt("bmi");
			int default_address_id = data.getInt("default_address_id");
			String memo = data.getString("memo");
			// 疾病类别
			int disease_id = data.getInt("disease_id");
			// 历史疾病
			String his_disease = data.getString("his_disease");
			//分组疾病
			String group_disease=" ";
			int gender=1;
			String brithday=" ";
			try {
				group_disease=data.getString("group_disease");
				gender = data.getInt("gender");
				brithday = data.getString("brithday");
			}catch (Exception e2) {
				e2.printStackTrace();
			}
			ProfileEntity entity = UsersService.getInstance().getProfileByUserId(userid);
			if (entity == null) {
				entity=new ProfileEntity();
				entity.setDisease_id(disease_id);
				entity.setHigh(high);
				entity.setHis_disease(his_disease);
				entity.setIdcard(idcard);
				entity.setName(name);
				entity.setStatus(1);
				entity.setUser_id(userid);
				entity.setWeight(weight);
				entity.setAdd_time(new Timestamp(System.currentTimeMillis()));
				entity.setUpdate_time(new Timestamp(System.currentTimeMillis()));
				entity.setBmi(bmi);
				entity.setMemo(memo);
				entity.setDefault_address_id(default_address_id);
				entity.setBrithday(brithday);
				entity.setGender(gender);
				entity.setGroup_disease(group_disease);
				boolean b = UsersService.getInstance().addProfile(entity);
				if (b) {
					r = 1;
				} else {
					e = "数据库错误";
				}
			}else {
				entity.setDisease_id(disease_id);
				entity.setHigh(high);
				entity.setHis_disease(his_disease);
				entity.setIdcard(idcard);
				entity.setName(name);
				entity.setStatus(1);
				entity.setUser_id(userid);
				entity.setWeight(weight);
				entity.setAdd_time(new Timestamp(System.currentTimeMillis()));
				entity.setUpdate_time(new Timestamp(System.currentTimeMillis()));
				entity.setBmi(bmi);
				entity.setMemo(memo);
				entity.setDefault_address_id(default_address_id);
				entity.setBrithday(brithday);
				entity.setGender(gender);
				entity.setGroup_disease(group_disease);
				boolean b = UsersService.getInstance().updateProfile(entity);
				if (b) {
					r = 1;
				} else {
					e = "数据库错误";
				}
			}
			break;
		case "set":
			int user_id = data.getInt("user_id");
			name = data.getString("name");
			idcard = data.getString("idcard");
			high = StrUtil.getFloat(data.getString("high"), 0.0f);
			weight = StrUtil.getFloat(data.getString("weight"), 0.0f);
			bmi = data.getInt("bmi");
			default_address_id = data.getInt("default_address_id");
			memo = data.getString("memo");
			// 疾病类别
			disease_id = data.getInt("disease_id");
			// 历史疾病
			his_disease = data.getString("his_disease");
			//分组疾病
			group_disease=" ";
			gender=1;
			brithday=" ";
			try {
				group_disease=data.getString("group_disease");
				gender = data.getInt("gender");
				brithday = data.getString("brithday");
			}catch (Exception e2) {
				e2.printStackTrace();
			}
			entity = UsersService.getInstance().getProfileByUserId(data.getInt("user_id"));
			if (entity == null) {
				entity=new ProfileEntity();
				entity.setDisease_id(disease_id);
				entity.setHigh(high);
				entity.setHis_disease(his_disease);
				entity.setIdcard(idcard);
				entity.setName(name);
				entity.setStatus(1);
				entity.setUser_id(user_id);
				entity.setWeight(weight);
				entity.setAdd_time(new Timestamp(System.currentTimeMillis()));
				entity.setUpdate_time(new Timestamp(System.currentTimeMillis()));
				entity.setBmi(bmi);
				entity.setMemo(memo);
				entity.setDefault_address_id(default_address_id);
				entity.setBrithday(brithday);
				entity.setGender(gender);
				entity.setGroup_disease(group_disease);
				boolean b = UsersService.getInstance().addProfile(entity);
				if (b) {
					r = 1;
				} else {
					e = "数据库错误";
				}
			}else {
				entity.setDisease_id(disease_id);
				entity.setHigh(high);
				entity.setHis_disease(his_disease);
				entity.setIdcard(idcard);
				entity.setName(name);
				entity.setStatus(1);
				entity.setUser_id(user_id);
				entity.setWeight(weight);
				entity.setAdd_time(new Timestamp(System.currentTimeMillis()));
				entity.setUpdate_time(new Timestamp(System.currentTimeMillis()));
				entity.setBmi(bmi);
				entity.setMemo(memo);
				entity.setDefault_address_id(default_address_id);
				entity.setBrithday(brithday);
				entity.setGender(gender);
				entity.setGroup_disease(group_disease);
				boolean b = UsersService.getInstance().updateProfile(entity);
				if (b) {
					r = 1;
				} else {
					e = "数据库错误";
				}
			}
			break;
		case "find":
			user_id = data.getInt("user_id");
			UsersEntity user = UsersService.getInstance().findUserById(user_id);
			if (user.getIs_doc() == 0) {
				entity = UsersService.getInstance().getProfileByUserId(user_id);
				if (entity == null) {
					e = "无信息";
				} else {
					r = 1;
					BindInsulinEntity insulin=InsulinService.getInstance().findInsulinByPatientId(user_id);
					json.put("d", entity.toJson(user,insulin));
				}
			} else if (user.getIs_doc() == 1) {
				DocProfileEntity docProfileEntity = UsersService.getInstance().findDocProfileByUserId(user_id);
				if (docProfileEntity != null) {
					r = 1;
					JSONObject profile = new JSONObject();
					profile.put("user_id", docProfileEntity.getUser_id());
					profile.put("name", docProfileEntity.getName());
					profile.put("title", docProfileEntity.getTitle());
					profile.put("inst_id", docProfileEntity.getInst_id());
					InstitutionEntity keshi = InstService.getInstance().findById(docProfileEntity.getInst_id());
					if (keshi != null) {
						profile.put("keshi", keshi.getName());
					}
					InstitutionEntity bumen = InstService.getInstance().findById(keshi.getPid());
					if (keshi != null) {
						profile.put("bumen", bumen.getName());
					}
					InstitutionEntity yiyuan = InstService.getInstance().findById(bumen.getPid());
					profile.put("yiyuan", yiyuan.getName());
					profile.put("memo", docProfileEntity.getMemo());
					profile.put("phone", user.getPhone());
					profile.put("head_url", SystemConfig.SERVER_DOMAIN + user.getHead_url());
					profile.put("is_doc", 1);
					profile.put("gender", docProfileEntity.getGender());
					profile.put("good",docProfileEntity.getGood());
					profile.put("nickname",docProfileEntity.getNickname());
					json.put("d", profile);
				}
			}
			break;
		case "add_address":
			String address = data.getString("address");
			name = data.getString("name");
			phone = data.getString("phone");
			user_id = data.getInt("user_id");
			UserAddressEntity addressEntity = new UserAddressEntity();
			addressEntity.setAddress(address);
			addressEntity.setStatus(1);
			addressEntity.setUser_id(user_id);
			addressEntity.setName(name);
			addressEntity.setPhone(phone);
			ProfileEntity profileEntity = UsersService.getInstance().getProfileByUserId(user_id);
			if (profileEntity == null) {
				e = "用户信息不存在";
			} else {
				boolean b = UsersService.getInstance().addAddress(profileEntity, addressEntity);
				r = 1;
			}
			break;
		case "update_address":
			int address_id = data.getInt("address_id");
			address = data.getString("address");
			user_id = data.getInt("user_id");
			int status = data.getInt("status");
			name = data.getString("name");
			phone = data.getString("phone");
			addressEntity = UsersService.getInstance().findAddressById(address_id);
			addressEntity.setAddress(address);
			addressEntity.setStatus(status);
			addressEntity.setUser_id(user_id);
			addressEntity.setName(name);
			addressEntity.setPhone(phone);
			boolean b = UsersService.getInstance().updateAddress(addressEntity);
			if (b) {
				r = 1;
			} else {
				e = "数据库错误";
			}
			break;
		case "list_address":
			user_id = data.getInt("user_id");
			List<UserAddressEntity> addList = UsersService.getInstance().findAddressByUserId(user_id);
			JSONArray array = new JSONArray();
			for (UserAddressEntity ad : addList) {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("id", ad.getId());
				jsonObject.put("address", ad.getAddress());
				jsonObject.put("user_id", ad.getUser_id());
				jsonObject.put("name", ad.getName());
				jsonObject.put("phone", ad.getPhone());
				array.add(jsonObject);
			}
			r = 1;
			json.put("d", array);
			break;
		}
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}
}
